package com.chahu.controller;

import com.chahu.common.aop.LogAnnotation;
import com.chahu.common.cache.Cache;
import com.chahu.entity.Archives;
import com.chahu.service.ArticleService;
import com.chahu.vo.ArticleVo;
import com.chahu.vo.Result;
import com.chahu.vo.params.ArticleParam;
import com.chahu.vo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping
    public Result<List<ArticleVo>> articles(@RequestBody PageParams pageParams){
        List<ArticleVo> articles = articleService.listArticlesPage(pageParams);
        return Result.success(articles);
    }

    @PostMapping("/hot")
    @Cache(expire = 5*60*1000, name = "hot_article")
    public Result<List<ArticleVo>> hotArticle(){
        int limit = 5;
        List<ArticleVo> articleVos = articleService.hotArticle(limit);
        return Result.success(articleVos);
    }

    @PostMapping("/new")
    public Result<List<ArticleVo>> newArticles(){
        int limit = 5;
        List<ArticleVo> articleVos = articleService.newArticles(limit);
        return Result.success(articleVos);
    }

    @PostMapping("/listArchives")
    public Result<List<Archives>> listArchives(){
        List<Archives> archives = articleService.listArchives();
        return Result.success(archives);
    }

    @PostMapping("/view/{id}")
    public Result findArticleById(@PathVariable("id") Long articleId){
        return articleService.findArticleById(articleId);
    }

    //  @RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的)；
    //  而最常用的使用请求体传参的无疑是POST请求了，所以使用@RequestBody接收数据时，一般都用POST方式进行提交。
    @PostMapping("publish")
    public Result publish(@RequestBody ArticleParam articleParam){
        return articleService.publish(articleParam);
    }

    @PostMapping
    @LogAnnotation(module = "文章", operation = "获取文章列表")
    public Result listArticle(@RequestBody PageParams pageParams){
        return articleService.listArticle(pageParams);
    }
}
